package com.ontheroad.mysql.central.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.modoumama.service.base.impl.BaseServiceImpl;

import com.ontheroad.central.model.CentralBindEntity;
import com.ontheroad.central.service.CentralBindTopicService;
import com.ontheroad.mysql.central.mapper.CentralBindTopicMapper;
import com.ontheroad.mysql.central.mapper.CentralTopicMapper;
import com.ontheroad.mysql.user.mapper.UserTopicMapper;
import com.ontheroad.user.model.UserEntity;
import com.ontheroad.utils.UUIDUtil;

/*import com.ontheroad.utils.SmsUtil;*/

/**
 * Created by kedong on 2017/6/25 0021.
 */
@Service
@Transactional
public class CentralBindTopicServiceImpl extends
		BaseServiceImpl<CentralBindEntity, String> implements
		CentralBindTopicService {

	private Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private CentralBindTopicMapper centralBindTopicMapper;

	@Autowired
	private UserTopicMapper userTopicMapper;
	
	@Autowired
	private CentralTopicMapper centralTopicMapper;

	public HashMap<String, Object> bind(CentralBindEntity centralBindEntity)
			throws Exception {
		HashMap<String, Object> bean = new HashMap<String, Object>();
		log.info("添加绑定信息");
		if (centralBindEntity.getUser() == null
				| centralBindEntity.getCentral() == null) {
			bean.put("code", 1);
			bean.put("message", "参数不能为空");
		}
		// 手机号是否存在
		UserEntity userEntity = userTopicMapper
				.getUserByMobile(centralBindEntity.getUser());
		if (userEntity == null) {
			bean.put("code", 1);
			bean.put("message", "手机号不存在");
			return bean;
		}
//		if(centralTopicMapper.findCentralByMac(centralBindEntity.getCentral())==null){
//			bean.put("code", 1);
//			bean.put("message", "中控不存在");
//			return bean;
//		}
		int count=centralBindTopicMapper.findCount(centralBindEntity);
		if(count>0){
			centralBindEntity.setIsBind(true);
			centralBindTopicMapper.modifyModel(centralBindEntity);
			List<String> centrals = centralBindTopicMapper
					.findMacByUser(centralBindEntity.getUser());
			bean.put("code", 0);
			bean.put("message", "success");
			bean.put("centrals", centrals);
			return bean;
		}
		centralBindEntity.setId(UUIDUtil.generate());
		centralBindEntity.setUpdateTime(new Date());
		centralBindEntity.setCreateTime(new Date());
		centralBindEntity.setIsEnable(true);
		centralBindEntity.setIsBind(true);
	    centralBindTopicMapper.insertModel(centralBindEntity);
		List<String> centrals = centralBindTopicMapper
				.findMacByUser(centralBindEntity.getUser());
		bean.put("code", 0);
		bean.put("message", "success");
		bean.put("centrals", centrals);
		return bean;
	}

	public HashMap<String, Object> unbind(CentralBindEntity centralBindEntity)
			throws Exception {
		HashMap<String, Object> bean = new HashMap<String, Object>();
		log.info("解除绑定信息");
		if (centralBindEntity.getUser() == null
				| centralBindEntity.getCentral() == null) {
			bean.put("code", 1);
			bean.put("message", "参数不能为空");
		}
		// 手机号是否存在
		UserEntity userEntity = userTopicMapper
				.getUserByMobile(centralBindEntity.getUser());
		if (userEntity == null) {
			bean.put("code", 1);
			bean.put("message", "手机号未注册");
			return bean;
		}
		centralBindEntity.setIsBind(false);
		centralBindTopicMapper.modifyModel(centralBindEntity);
		List<String> centrals = centralBindTopicMapper
				.findMacByUser(centralBindEntity.getUser());
		bean.put("code", 0);
		bean.put("message", "success");
		bean.put("centrals", centrals);
		return bean;
	}
}
